<?php
namespace App\Service;

use Illuminate\Support\Facades\DB;

class BaseService
{
    //基础类

    protected static $db;
    protected static $dbStaff = 'staff'; //员工
    protected static $dbStRole = 'st_role'; //角色
    protected static $dbStAuth = 'st_auth'; //权限
    protected static $dbStDepart = 'st_depart'; //部门
    protected static $dbStLogin = 'st_login'; //登录
    protected static $dbStAction = 'st_action'; //功能
    protected static $dbConfigItem = 'config_item'; //项目
    protected static $dbConfigTeam = 'config_team'; //团队
    protected static $dbConfigWorkHour = 'config_work_hour'; //工时项
    protected static $dbConfigWhUid = 'config_wh_uid'; //默认工时
    protected static $dbConfigWhCnt = 'config_wh_cnt'; //工时选项
    protected static $dbYwTask = 'yw_task'; //任务
    protected static $dbYwWh = 'yw_work_hour'; //工时
    protected static $dbYwWhStatus = 'yw_wh_status'; //工时状态
    protected static $dbYwWhItem = 'yw_wh_item'; //工时项目

    //项目状态
    protected static $itemStatuss = [
        1 => '新设立', '进行中', '已完成',
    ];
    //工时分类
    protected static $whTypes = [
        1 => '研发工时', '销售业务处理工时', '管理工时',
    ];
    //工时状态：1草稿，2保存，3新提交，4拒绝，5通过，6进行，7完成
    protected static $whStatuss = [
        1 => '草稿', '保存', '提交', '拒绝', '通过', '进行', '完成',
    ];

    //连接数据库
    public static function dbConn($db)
    {
        return DB::connection('mysql')->table($db);
    }

    //返回格式
    public static function toback($code = 0, $msg = '', $data = [], $total = 0)
    {
        $msg = $msg ? $msg : '成功';
        return [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
            'total' => $total,
        ];
    }

    //计算
    public static function getDelay($start, $end)
    {
        if (!$start || !$end) {
            return self::toback(-2, '没有参数');
        }
        if ($start > $end) {
            return self::toback(-2, '开始时间大于结束时间');
        }
        //时间
        $hours = []; $hour01 = []; $hour02 = [];
        for ($i = 8; $i <= 12; $i ++) {
            if ($i == 12) {
                $hours[] = '12:00';
                $hour01[] = '12:00';
            } else {
                $ii = strlen($i)<2 ? '0'.$i : $i;
                for ($j = 0; $j <= 5; $j ++) {
                    $jj = $j == 0 ? '00' : $j * 10;
                    $hours[] = $ii.':'.$jj;
                    $hour01[] = $ii.':'.$jj;
                }
            }
        }
        for ($i = 13; $i <= 17; $i ++) {
            if ($i == 17) {
                $hours[] = '17:00';
                $hour02[] = '17:00';
            } else {
                $ii = strlen($i)<2 ? '0'.$i : $i;
                for ($j = 0; $j <= 5; $j ++) {
                    $jj = $j == 0 ? '00' : $j * 10;
                    $hours[] = $ii.':'.$jj;
                    $hour02[] = $ii.':'.$jj;
                }
            }
        }
        //计算
        $si = 0; $ei = 0;
        foreach ($hours as $k => $v) {
            if ($v == $start) $si = $k;
            if ($v == $end) $ei = $k;
        }
        $delay = ($ei - $si) * 10;
        if (in_array($start, $hour01) && in_array($end, $hour02)) {
            $delay = $delay - 10;
        }
        $shi = floor($delay / 60);
        $fen = $delay - $shi * 60;
        $arys = [
            'fen' => $delay,
            'str' => $shi ? $shi.'小时'.$fen.'分钟' : $fen.'分钟',
        ];
        return self::toback(0, '', $arys);
    }

    //分转时
    public static function shiByFen($fen)
    {
        $shi = floor($fen / 60);
        $fen = $fen - $shi * 60;
        return $shi ? $shi.'小时'.$fen.'分钟' : $fen.'分钟';
    }
}